import 'package:flutter/material.dart' hide SearchController;

import '../../utils/util.dart';
import 'index.dart';
import 'widgets/widgets.dart';

class SearchPage extends GetView<SearchController> {
  const SearchPage({Key? key}) : super(key: key);

  // 主视图
  Widget _buildView() {
    var state = controller.state;
    return !state.isSearching ? SearchHistoryView() : SearchResultView();
  }

  @override
  Widget build(BuildContext context) {
    var state = controller.state;
    return GetBuilder<SearchController>(
      builder: (_) {
        return Scaffold(
          appBar: AppBar(
            backgroundColor: ColorsUtil.theme,
            leading: const SizedBox(),
            flexibleSpace: SafeArea(
              child: Row(
                children: [
                  IconButton(
                    onPressed: Get.back,
                    icon: Icon(
                      IconUtil.arrow_left2,
                      color: Colors.white,
                      size: 20.r,
                    ),
                  ),
                  Expanded(
                    child: Container(
                      height: 40.r,
                      padding: EdgeInsets.only(right: 10.r),
                      child: TextField(
                        controller: state.inputCtrl,
                        textAlignVertical: TextAlignVertical.center,
                        decoration: InputDecoration(
                          hintText: '小微企业',
                          hintStyle: TextStyle(
                            color: ColorsUtil.placeholder,
                            fontSize: 14.sp,
                          ),
                          prefixIcon: Icon(
                            IconUtil.search,
                            size: 20.r,
                            color: ColorsUtil.placeholder,
                          ),
                          suffixIcon: InkWell(
                            onTap: controller.actionSearch,
                            child: Container(
                              width: 60.r,
                              alignment: Alignment.center,
                              padding: EdgeInsets.symmetric(horizontal: 10.r),
                              margin: const EdgeInsets.fromLTRB(0, 5, 5, 5),
                              decoration: BoxDecoration(
                                color: ColorsUtil.theme,
                                borderRadius: BorderRadius.circular(20.r),
                              ),
                              child: Text(
                                '搜索',
                                style: TextStyle(
                                  color: Colors.white,
                                  fontSize: 15.sp,
                                ),
                              ),
                            ),
                          ),
                          border: OutlineInputBorder(
                            borderRadius: BorderRadius.circular(20.r),
                            borderSide: BorderSide.none,
                          ),
                          filled: true,
                          fillColor: Colors.white,
                          isCollapsed: true,
                        ),
                      ),
                    ),
                  ),
                ],
              ),
            ),
            bottom: PreferredSize(
                preferredSize: Size.fromHeight(50.r),
                child: TPTabBar(
                  controller: state.tabCtrl,
                )),
          ),
          body: SafeArea(
            child: _buildView(),
          ),
        );
      },
    );
  }
}
